<template>
  <view class="register-container">
    <view class="register-form">
      <view class="logo-container">
        <h2>温室卷帘门控制系统</h2>
      </view>
      <view class="logo-container">
        <image src="/static/logo.png" class="register-logo" />
      </view>
      <view class="input-group">
        <text class="label">用户名</text>
        <view class="input-field">
          <input type="text" placeholder="请输入用户名" v-model="username" />
        </view>
      </view>
      <view class="input-group">
        <text class="label">密码</text>
        <view class="input-field">
          <input type="password" placeholder="请输入密码" v-model="password" />
        </view>
      </view>
      <view class="input-group">
        <text class="label">确认密码</text>
        <view class="input-field">
          <input type="password" placeholder="请再次输入密码" v-model="confirmPassword" />
        </view>
      </view>
      <button class="register-button" @click="register">注册</button>
      <view class="login-link">
        <text class="link-text" @click="goToLogin">已有账号？登录</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: ''
    };
  },
  methods: {
    register() {
      // 验证用户名、密码和确认密码是否为空
      if (!this.username || !this.password || !this.confirmPassword) {
        uni.showToast({
          title: '用户名、密码和确认密码不能为空',
          icon: 'none'
        });
        return;
      }
      // 验证密码和确认密码是否一致
      if (this.password !== this.confirmPassword) {
        uni.showToast({
          title: '两次输入的密码不一致',
          icon: 'none'
        });
        return;
      }

      // 这里可以添加实际的注册请求逻辑，例如使用 uni.request 发送请求到后端服务器
      console.log(`用户名: ${this.username}, 密码: ${this.password}`);
      
      // 注册成功提示
      uni.showToast({
        title: '注册成功',
        icon: 'success',
        duration: 2000, // 显示时间延长，确保用户能看到提示
        success: () => {
          // 提示消失后跳转登录页
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/login/login001' // 根据 pages.json 配置的路径
            });
          }, 2000); // 等待提示显示完毕
        }
      });
    },
    goToLogin() {
      // 直接跳转到登录页面
      uni.navigateTo({
        url: '/pages/login/login001' // 根据 pages.json 配置的路径
      });
    }
  }
};
</script>

<style scoped>
/* 清除默认样式 */
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
  box-sizing: border-box;
}

.register-form {
  background-color: #ffffff;
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.logo-container {
  text-align: center;
  margin-bottom: 24px;
}

.register-logo {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin: 0 auto;
}

.input-group {
  margin-bottom: 16px;
}

.label {
  display: block;
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.input-field {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 16px;
  box-sizing: border-box;
}

.register-button {
  width: 100%;
  padding: 10px;
  background-color: #007bff;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  margin-bottom: 16px;
}

.register-button:active {
  background-color: #0056b3;
}

.login-link {
  display: flex;
  justify-content: center;
  font-size: 14px;
  color: #007bff;
}

.link-text {
  text-decoration: none;
  cursor: pointer;
}

.link-text:hover {
  text-decoration: underline;
}
</style>